package com.emm.yixun.website.controller.router;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.emm.yixun.anotations.AvoidDuplicateSubmission;
import com.emm.yixun.common.model.BusinessAbnorma;
import com.emm.yixun.common.model.NeedReceiveRefund;
import com.emm.yixun.common.model.dto.BusinessAbnormaDto;
import com.emm.yixun.common.model.dto.NeedReceiveRefundDto;
import com.emm.yixun.common.model.enums.AuditStatusEnum;
import com.emm.yixun.common.model.enums.BusinessAbnormaEnum;
import com.emm.yixun.common.model.enums.BusinessTypeEnum;
import com.emm.yixun.common.model.enums.DocTypeEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.marketing.service.BusinessAbnormaServiceFacade;
import com.emm.yixun.marketing.service.NeedReceiveRefundServiceFacade;

/*
 * 映射规范：/router/一级模块简称/二级模块简称/页面简称
 * 营销模块路由
 * */
@Controller
@RequestMapping("/router/marketing/")
public class MarketingRouterController {
	
	@Autowired
	private BusinessAbnormaServiceFacade businessAbnormaServiceFacade;
	@Autowired
	private NeedReceiveRefundServiceFacade needReceiveRefundServiceFacade;
	/*
	 * 跳转延期签约申请页面
	 * 参数为签约ID和应收款ID,其中应收款ID非必填
	 * */
	@RequestMapping("receivables/deferredPayment")
	@AvoidDuplicateSubmission(needSaveToken = true)
	public String deferredPayment(String signedId,String needReceiveRefundId,ModelMap map) {
		map.put("signedId", signedId);
		map.put("needReceiveRefundId", StringUtils.isNotBlank(needReceiveRefundId)?needReceiveRefundId:"");
		boolean showGrid = showHisAuditGrid(Long.valueOf(signedId));
		map.put("showGrid", showGrid);
		if(StringUtils.isNotBlank(needReceiveRefundId)) {
			map.put("businessId", needReceiveRefundId);
		}else {
			NeedReceiveRefundDto nrrDto = new NeedReceiveRefundDto();
			nrrDto.setRelationId(Long.valueOf(signedId));
			nrrDto.setBusinessType(BusinessTypeEnum.SIGNED.getNo());
			nrrDto.setDocType(DocTypeEnum.COLLECTION.getNo());
			ResponseList<NeedReceiveRefund> nrrListRes = needReceiveRefundServiceFacade.findByDto(nrrDto);
			if(nrrListRes.isSuccess() && null != nrrListRes.getEntity()
					&& nrrListRes.getEntity().size() > 0) {
				List<NeedReceiveRefund> nrrList = nrrListRes.getEntity();
				String businessIds = "";
				for(int ii=0; ii<nrrList.size(); ii++) {
					NeedReceiveRefund nrr = nrrList.get(ii);
					businessIds += nrr.getId();
					if(ii != nrrList.size() - 1) {
						businessIds += ",";
					}
				}
				map.put("businessId", businessIds);
			}
		}
		
		return "/customer/signed/deferredPayment";
	}
	
	/*
	 *跳转延期签约详情页面
	 *参数为异常流程表(mkt_tr_business_abnorma)ID
	 * */
	@RequestMapping("receivables/deferredPaymentDetail")
	public String deferredPaymentDetail(String id,ModelMap map,String processFlag) {
		map.put("id", id);
		Response<BusinessAbnorma> baRes = businessAbnormaServiceFacade.findById(Long.parseLong(id));
		map.put("showGrid", false);
		if(null != baRes){
			BusinessAbnorma ba = baRes.getEntity();
			map.put("procInstId", ba.getProcInstId());
			map.put("businessId", ba.getBusinessId());
			//判断是否重复,需要使用收款的签约ID
			Response<NeedReceiveRefund> nrrRes = needReceiveRefundServiceFacade.findById(ba.getBusinessId());
			if(null != nrrRes && null != nrrRes.getEntity()){
				boolean showGrid = showHisAuditGrid(nrrRes.getEntity().getRelationId());
				map.put("showGrid", showGrid);
			}
		}
		map.put("processFlag", processFlag);
		return "/customer/signed/deferredPaymentView";
	}
	
	/**
	 * 判断是否显示同房同流程的其他审批记录
	 * @param businessId
	 * @return
	 */
	public boolean showHisAuditGrid(long businessId) {
		boolean showGrid = false;
		//延期收款绑定的是代收款业务ID,需通过签约ID查出所有代收款记录,再去匹配是否存在已发起或已处理流程
		NeedReceiveRefundDto nrrDto = new NeedReceiveRefundDto();
		nrrDto.setRelationId(businessId);
		nrrDto.setBusinessType(BusinessTypeEnum.SIGNED.getNo());
		nrrDto.setDocType(DocTypeEnum.COLLECTION.getNo());
		ResponseList<NeedReceiveRefund> nrrListRes = needReceiveRefundServiceFacade.findByDto(nrrDto);
		if(nrrListRes.isSuccess() && null != nrrListRes.getEntity()
				&& nrrListRes.getEntity().size() > 0) {
			List<NeedReceiveRefund> nrrList = nrrListRes.getEntity();
			//判断所有代收中有没有存在审核中的记录
			boolean flagAudit = false;
			boolean flagComplete = false;
			for(NeedReceiveRefund nrr: nrrList) {
				BusinessAbnormaDto businessAbnormaDto = new BusinessAbnormaDto();
				businessAbnormaDto.setBusinessId(nrr.getId());
				businessAbnormaDto.setBusinessType(BusinessAbnormaEnum.BusinessAbnorma_12.getNo());
				//审核条件去掉,根据状态判断
				//1、待提交: 无暂存功能,不存在待提交延期收款审批
				//2、审核中: 存在未审核完成记录,不允许发起
				//3、已退回: 退回后,无法再次发起
				//4、已通过: 已通过,再次提醒是否需要再次发起
				businessAbnormaDto.setOrders("create_time desc");
				ResponseList<BusinessAbnorma> businessAbnormas = businessAbnormaServiceFacade.findByDto(businessAbnormaDto);
				if(businessAbnormas.isSuccess() && null != businessAbnormas.getEntity()
						&& businessAbnormas.getEntity().size() != 0) {
					List<BusinessAbnorma> baList = businessAbnormas.getEntity();
					BusinessAbnorma ba = baList.get(0);
					String auditStatus = ba.getAuditStatus();
					if(AuditStatusEnum.AUDIT.getNo().equals(auditStatus)) {
						flagAudit = true;
						break;
					}else if(AuditStatusEnum.SUCCESS.getNo().equals(auditStatus)
							|| AuditStatusEnum.REJECT.getNo().equals(auditStatus)) {
						flagComplete = true;
						break;
					}
				}
			}
			if(flagAudit || flagComplete) {
				showGrid = true;
			}
		}
		return showGrid;
	}
	
	public static void main(String[] args) {
		BigDecimal ratio = new BigDecimal(1233457).divide(new BigDecimal(10000), 5, BigDecimal.ROUND_HALF_UP);
		//格式化比例
		DecimalFormat decimalFormat = new DecimalFormat("#.####");
		System.out.println(decimalFormat.format(ratio));
	}
}
